package br.edu.ufcg.sig;

import java.util.ArrayList;

import br.edu.ufcg.sig.query.ColumnInfo;
import br.edu.ufcg.sig.query.QueryExecutorImpl;
import br.edu.ufcg.sig.query.QueryExecutorInterface;
import br.edu.ufcg.sig.query.QueryTable;


public class ConsultaBean {

	private String query;

	private QueryExecutorInterface queryExecutor = new QueryExecutorImpl();
	
	
	public String executeQuery() {
		QueryTable result;
		try {
			result = queryExecutor.executeQuery(query);
			adicionarAoMapa(result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}
	
	private void adicionarAoMapa(QueryTable result) {
		ArrayList<ColumnInfo> geos = result.getColumnsWithTag("_geom");
		ArrayList<ColumnInfo> popups = result.getColumnsWithTag("_popup");

		for (String[] row : result.getValues()) {
			for (String s : row) {
				System.out.println("ROW[s] = " + s);
			}
			String popupsLabels = "";
			for (ColumnInfo popup : popups) {
				popupsLabels += popup.getName() + ": " + row[popup.getIndex()]
						+ "<br>";
			}
			System.out.println("POPUPS: " + popupsLabels);

			ArrayList<String> geometries = new ArrayList<String>();
			for (ColumnInfo geo : geos) {
				geometries.add(row[geo.getIndex()]);
			}

//			addPoligons(geometries, popupsLabels);
		}
	}
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}
	
}
